import path from "path"
import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import vueDevTools from "vite-plugin-vue-devtools"
import dts from "vite-plugin-dts"

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement: tag => tag.startsWith('h-icon-')
        }
      }
    }),
    vueDevTools(),
    dts({
      entryRoot: "src",
      outDir: ["./es"],
      tsconfigPath: "tsconfig.app.json",
    }),
  ],
  css: {
    postcss: "./postcss.config.js",
  },
  build: {
    lib: {
      entry: { index: "./src/index.ts", resolver: "./src/utils/resolver.ts" },
    },
    rollupOptions: {
      external: (id) => {
        // 全文匹配
        const ids = ["@/ant-design-vue", "@/icon", "vue"]
        if (ids.includes(id)) {
          return true
        }

        // 前缀匹配
        const prefixs = ["@/ant-design-vue", "@/icon"]
        return prefixs.some((item) => id.startsWith(item))
      },
      output: [
        {
          format: "es",
          entryFileNames: "[name].js",
          preserveModules: true,
          preserveModulesRoot: "src",
          exports: "named",
          dir: "./es",
        },
        {
          format: "cjs",
          entryFileNames: "[name].js",
          preserveModules: true,
          preserveModulesRoot: "src",
          exports: "named",
          dir: "./lib",
        },
      ],
    },
  },
})
